﻿// -------------------------------------------------------------------------------------------------
// <copyright file="TVDBSiteMirrorFactory.cs" company="HeterogeneousComputing.com">
// Creative Commons Attribution-ShareAlike 3.0 Unported License
// </copyright>
//
// <author>Jock</author>
//
// <date>04/08/2011</date>
//
// <summary>Implements the tvdb site mirror factory class</summary>
// -------------------------------------------------------------------------------------------------
namespace TheTVDBScraper
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;

    /// <summary>
    /// Factory class that parses the results from TheTVDB
    /// and builds a List of TVDBSiteMirror objects from the output.
    /// </summary>
    public static class TVDBSiteMirrorFactory
    {
        #region Internal Operations
        /// <summary>
        /// Operation that parses the XML results of the WebClient call
        /// to TheTVDB. Returns a List of TVDBSiteMirror.
        /// </summary>
        /// <param name="xmlResults">
        /// A string containing the XML results from TheTVDB.
        /// </param>
        /// <returns>
        /// Returns a List of TVDBSiteMirror parsed out of the supplied XML.
        /// </returns>
        internal static List<TVDBSiteMirror> Parse(string xmlResults)
        {
            List<TVDBSiteMirror> results = new List<TVDBSiteMirror>();
            XElement resultsDoc = XElement.Load(new StringReader(xmlResults));

            IEnumerable<XElement> mirrorSites =
                from item in resultsDoc.Descendants("Mirror")
                orderby (string)item.Element("id")
                select item;

            foreach (var mirror in mirrorSites) 
            {
                TVDBSiteMirror tvdbSiteMirror = new TVDBSiteMirror();
                tvdbSiteMirror.MirrorId = mirror.Element("id").Value;
                tvdbSiteMirror.MirrorPath = mirror.Element("mirrorpath").Value;
                string typemaskString = mirror.Element("typemask").Value;
                CreateTypeMask(tvdbSiteMirror, typemaskString);
                results.Add(tvdbSiteMirror);
            }

            return results;
        }

        #endregion Internal Operations

        #region Private Operations
        /// <summary>
        /// Operation that parses the string containing the TypeMask returned from
        /// TheTVDB for the mirror site.
        /// </summary>
        /// <param name="tvdbSiteMirror">
        /// The new Mirror Site Object we are creating.
        /// </param>
        /// <param name="typemaskString">
        /// The string typemask.
        /// </param>
        private static void CreateTypeMask(TVDBSiteMirror tvdbSiteMirror, string typemaskString)
        {
            int typemaskInt = -1;
            try
            {
                typemaskInt = Convert.ToInt32(typemaskString);
                switch (typemaskInt)
                {
                    case 7:
                        tvdbSiteMirror.TypeMask = new List<TVDBScraper.MirrorsTypeMaskOptionsEnum> 
                        { 
                            TVDBScraper.MirrorsTypeMaskOptionsEnum.XML_FILES,
                            TVDBScraper.MirrorsTypeMaskOptionsEnum.BANNER_FILES, TVDBScraper.MirrorsTypeMaskOptionsEnum.ZIP_FILES 
                        };
                        break;
                    case 4:
                        tvdbSiteMirror.TypeMask = new List<TVDBScraper.MirrorsTypeMaskOptionsEnum> 
                        { 
                            TVDBScraper.MirrorsTypeMaskOptionsEnum.ZIP_FILES 
                        };
                        break;
                    case 3:
                        tvdbSiteMirror.TypeMask = new List<TVDBScraper.MirrorsTypeMaskOptionsEnum> 
                        { 
                            TVDBScraper.MirrorsTypeMaskOptionsEnum.XML_FILES,
                            TVDBScraper.MirrorsTypeMaskOptionsEnum.BANNER_FILES
                        };
                        break;
                    case 2:
                        tvdbSiteMirror.TypeMask = new List<TVDBScraper.MirrorsTypeMaskOptionsEnum> 
                        { 
                            TVDBScraper.MirrorsTypeMaskOptionsEnum.BANNER_FILES 
                        };
                        break;
                    case 1:
                        tvdbSiteMirror.TypeMask = new List<TVDBScraper.MirrorsTypeMaskOptionsEnum> 
                        { 
                            TVDBScraper.MirrorsTypeMaskOptionsEnum.XML_FILES 
                        };
                        break;
                    case 0:
                        tvdbSiteMirror.TypeMask = new List<TVDBScraper.MirrorsTypeMaskOptionsEnum> 
                        { 
                            TVDBScraper.MirrorsTypeMaskOptionsEnum.NULL 
                        };
                        break;
                }
            }
            catch (FormatException e)
            {
                Console.WriteLine("Input string is not a sequence of digits. " + e);
            }
            catch (OverflowException e)
            {
                Console.WriteLine("The number cannot fit in an Int32." + e);
            }
        }
        #endregion
    }
}

/// -------------------------------------------------------------------------------------------------
// End of TVDBSiteMirrorFactory.cs
/// -------------------------------------------------------------------------------------------------
